<?PHP
#
# author: Cody Sorgenfrey
# (c)2012 South Hill Calvary Chapel
#

include_once("../mysql/database_info.php");
include_once("common.php");
include_once("../mysql/common.php");

if ($_REQUEST['librarian_cookie'] != 1) header("location: http://www.southhillcalvary.org/404error.php");

function print_book_table($copies, $book) {
	
	$book = mysql_fetch_array($book);
	
	$num = mysql_num_rows($copies);
	
	$currDate = strtotime(date(Y).date(m).date(d));
	
	if ($num == 1) {
	
		printf ("<div style='float:right;'><p>Displaying %d result</p></div>\n", $num); 
	} 
	else {
		
		printf ("<div style='float:right;'><p>Displaying %d results</p></div>\n", $num);
	}	
	printf ("<table style='width:%s;'>\n", "100%");
        
    printf ("\t<tr>\n\t\t<td style='padding:15px;'><h3>Title</h3></td><td style='padding:15px;'><h3>Availability</h3></td>\n\t</tr>\n");
    
	while ($row = mysql_fetch_array($copies)){
		
        if ($i && ($row['orig_copies'] > 1)){
			printf ("\t<tr class='dotted_lines row_odd' onclick='refreshPage(\"/librarydetail.php?title=%s&id=%s&oldId=%s\")'>\n",urlencode($row['title']), $row['id'], $book['id']);
        } 
        else {
        	printf ("\t<tr class='dotted_lines row_even' onclick='refreshPage(\"/librarydetail.php?title=%s&id=%s&oldId=%s\")'>\n",urlencode($row['title']), $row['id'], $book['id']);
        }
				
		printf ("\t\t<td style='padding:15px;'>%s</td>",$row['title']); 
		
		if ($row['overDue'] == 1){ 
				
			printf("<td style='padding:15px;'><font color='red'>%s/%s</font></a></td>\n",$row['copies_avail'], $book['copies_avail']);
		}
  		else {
				
			printf("<td style='padding:15px;'>%s/%s</td>\n",$row['copies_avail'], $book['copies_avail']);
		}
		printf ("\t</tr>\n");
       
        if ($i == 1){
        	$i = 0;
        } else {
        	$i = 1;
        }

	}
	printf("</table>");

}
function deleteAllBooks() {
	mysql_query("DROP TABLE `SHCCLibrary_ ".$_REQUEST['id']."");
	mysql_query("DELETE FROM SHCCLibrary WHERE id='".$_REQUEST['id']."'");
	printf("<script type='text/javascript'> refreshPage(\"/lendinglibrary.php?message=Deleted!\"); </script>");
}
function addNewCopyForm() {
	
	printf("<form style='margin-top:20px;' action='librarydetailmultiple.php'>\n");
	
	printf("\t<input type='text' name='numCopies' placeholder='Number of Copies' \>\n");
	
	printf("\t<input type='hidden' name='title' value='%s' \>\n", stripcslashes($_REQUEST['title']));
	
	printf("\t<input type='hidden' name='id' value='%s' \>\n", $_REQUEST['id']);
	
	printf("\t<input type='submit' value='Add' \>\n");
	
	printf("</p></form>\n\n");
}
function addNewCopy($num) {

	$book = mysql_fetch_array(mysql_query("SELECT * FROM `SHCCLibrary` WHERE `id`='".$_REQUEST['id']."'"));
	
	$newNumOrigCopies = $book['orig_copies'] + $num;
		
	$newNumCopiesAvail = $book['copies_avail'] + $num;
								
	for ($g=1; $g<=$num; $g++){
			
		mysql_query("INSERT INTO `SHCCLibrary_ ".$book['id']."` (title, author, orig_copies, copies_avail, genre, who_has, checkout, due, email, overDue) VALUES ('" . $book['title'] . " - copy " . ($g+$book['orig_copies']) . "','" .$book['author'] . "','" . $newNumOrigCopies . "','1','" . $book['genre'] . "', 'Available', '', '', '', '0')");
		}
			
	mysql_query("UPDATE `SHCCLibrary` SET `orig_copies` = '". $newNumOrigCopies ."' WHERE `id` = '". $book['id'] ."'");
			
	mysql_query("UPDATE `SHCCLibrary` SET `copies_avail` = '". $newNumCopiesAvail ."' WHERE `id` = '". $book['id'] ."'");
		
	printf("<script type='text/javascript'>refreshPage(\"/librarydetailmultiple.php?message=Book(s)+Added!&title=%s&id=%s\");</script>\n\n", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id']);
}

function addNewCopyButton() {
	
	printf("<button onclick='refreshPage(\"/librarydetailmultiple.php?addCopies=1&title=%s&id=%s\")'>Add copy/copies</button>\n", urlencode(stripcslashes($_REQUEST['title'])), $_REQUEST['id']);
}
function backButton () {
	
	printf("<button style='float:right;' onclick='refreshPage(\"/lendinglibrary.php\")'>Back</button>\n\n");
}
function deleteAllButton() {
	
	printf("<button style='margin-bottom:10px;' onclick='warnDelete(\"ALL\",\"%s\",\"%s\",\"NULL\")'>Delete All Copies</button>\n", $_REQUEST['title'], $_REQUEST['id']);
}

$title = "Copies of \"" . stripcslashes($_REQUEST['title']) . "\"";
$meta_key='books, lend, borrow, christian bookstore';
$meta_desc="Page for viewing multiple copies of a book.";
startPage($title, $meta_key, $meta_desc, $morehtml, $morejava);

connect_select_test("SHCCLibrary");

if ($_REQUEST['message']) printf("<h4 style='color:red;'><b>%s</b></h4>", $_REQUEST['message']);

backButton (); 

deleteAllButton();

if ($_GET['deleteAll'] == TRUE) deleteAllBooks();

printf("<br>"); addNewCopyButton();

if ($_GET['addCopies'] == 1) addNewCopyForm();

if ($_GET['numCopies']) addNewCopy($_GET['numCopies']);

$copies = mysql_query("SELECT * FROM `SHCCLibrary_ ".$_REQUEST['id']."` ORDER BY `id` ASC");
$book = mysql_query("SELECT * FROM `SHCCLibrary` WHERE `id`='".$_REQUEST['id']."' ORDER BY `id` ASC");
print_book_table($copies, $book);

mysql_close();

endPage();
?>